package com.asset.dispoal.dataDetail.service.commom;

import com.asset.dispoal.dataDetail.domain.db.mapper.SysDataDetailMapper;
import com.asset.dispoal.dataDetail.dto.SysDataDetailRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;
import java.util.Map;

/**
 * @author qr
 * @date 2022/3/23$
 */
@Component
public class DataDetailInsertCommand implements BaseCommand<SysDataDetailRequest, Result> {

    @Autowired
    SysDataDetailMapper sysDataDetailMapper;

    @Autowired
    private BaseIdeable<String> idWorker;

    @Override
    public Result execute(SysDataDetailRequest request) {
        List<Map<String, Object>> maps = sysDataDetailMapper.queryTemplate();
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        for (Map<String, Object> map : maps) {
            map.put("id", idWorker.generateId());
            map.put("createUser", userId);
            map.put("customName", null);
            map.put("assetInstBatchId", request.getAssetInstBatchId());
            map.put("createTime", DateTimeUtil.currentDate());
        }
        sysDataDetailMapper.deleteByAssetInstBatchId(request.getAssetInstBatchId());
        sysDataDetailMapper.initialization(maps);
        return Result.ok();
    }
}
